package common;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.sql.*;
import ConnDatabase.*;


public class SelectTag2  extends BodyTagSupport  {
	
	private String name;
	private String index;
	private String valueName ;
	private String codeName;
	public int doStartTag() {
		return(EVAL_PAGE);
	}
	
	public int doEndTag() {
		try {
			BodyContent body = getBodyContent();
			JspWriter out = body.getEnclosingWriter(); //ok
			//JspWriter out = pageContext.getOut(); //ok
			String selectStr = body.getString();
			String codeValue = "";
			String valueValue = "";
			ConnPropertyDatabase conn = new ConnPropertyDatabase();
			ResultSet rs = conn.executeQuery(selectStr);
			String outStr = "\r\n\t<select name=" + name + ">";
			while (rs.next()) {
				if (index.equals("1")) {
					codeValue = rs.getString(codeName).trim();
					valueValue = rs.getString(valueName).trim();
				}  else  {
					valueValue = rs.getString(valueName).trim();
					codeValue = rs.getString(codeName).trim();
				}		
				outStr += "\r\n\t\t<option value=\"" + codeValue;
				outStr += "\">" + valueValue;
			}
			outStr += "\r\n\t</select>";	
			out.print(outStr);
		} catch (Exception e) {
			System.out.println("selecttag : do endtag" + e.getMessage());
		}	
		return(EVAL_PAGE);		
	}	
	
	public void setName(String setValue) {
		name = setValue;
	}	
	
	public void setValueName(String setValue) {
		valueName = setValue;
	}	
	
	public void setCodeName(String setValue) {
		codeName = setValue;	
	}
	
	public void setIndex(String setValue) {
		index = setValue;	
	}		
}



